<?php
namespace app\admin\controller;

use think\facade\View;
use think\Request;
class UserBalanceLog extends Base
{
    protected $ModelPath = 'UserBalanceLog';
    protected $model = null;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->con = "UserGoumaiMoney";
        $this->title = "用户" . \app\model\Base::$goumaiMoneyName;

        $module = $this->ModelPath;
        $this->model = new \app\admin\model\UserBalanceLog();

        View::assign(array(
            'module' => $module,
            'title' => $this->title,
            'con' => $this->con,
            'pageSize' => $this->pageSize
        ));
    }

    public function index()
    {
        if(request()->isPost()){
            $params = request()->param();
            $map = [];

            if(!empty($params['user_id'])){
                $map[] = ['user_id', '=', $params['user_id']];
            }
            if(!empty($params['type'])){
                $map[] = ['type', '=', $params['type']];
            }
            if(isset($params['phone'])){
                if(!empty($params['phone'])){
                    $list = \app\admin\model\User::where('phone', 'like', '%' . $params['phone'] . '%')->column('id');
                    if($list){
                        $map[] = ['user_id', 'in', $list];
                    }
                }
            }
//            if(!empty($params['name2'])){
//                $map[] = ['name2', 'like', "%" . $params['name2'] . "%"];
//            }
//            if(!empty($params['no'])){
//                $map[] = ['no', '=', $params['no']];
//            }
//            dd($map);
            $total = \app\admin\model\UserBalanceLog::where($map)->count();

            $pageIndex = max(input('page'), 1);
            $pageSize = $this->pageSize;
            $p = ($pageIndex - 1) * $pageSize;

            $list = $this->model
                ->where($map)
                // ->where('rec',1)

//
                ->append(['amount_format', 'before_format', 'type_format'])
                ->limit($p, $pageSize)
                ->order('id DESC')
//                ->fetchSql()
                ->select();
// var_dump($list);die;
            $arr['data'] = $list;
            $arr['code'] = 0;
            // var_dump(1);die;
            $arr['count'] = $total;
            $arr['limit'] = $pageSize;
            $arr['msg'] = "获取" . $this->title . "列表成功";
            return json($arr);
        }else{
            $type_lists = \app\model\Base::$walletLogDesArray;
            View::assign(compact('type_lists'));
            return View::fetch();
        }
    }
}